from time import sleep
import requests
from lxml import etree
import json
import pandas as pd

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69'
}
url = 'https://www.dongchedi.com/motor/pc/car/brand/select_series_v2?aid=1839&app_name=auto_web_pc'


# 定义主函数
def main():
    # 初始化一个空列表
    info_list = []
    # 遍历1到22
    for i in range(1, 22):
        # 初始化一个字典
        form = {
            # 设置fuel_form为4
            'fuel_form': 4,
            # 设置sort_new为hot_desc
            'sort_new': 'hot_desc',
            # 设置city_name为广州
            'city_name': '广州',
            # 设置limit为30
            'limit': 30,
            # 设置page为i
            'page': i
        }
        # 调用req_data函数，传入form参数，获取data
        data = req_data(form)
        # 调用handle_data函数，传入data参数，获取info_list
        info_list += handle_data(data)
        # 休眠1秒
        sleep(1)
    # 调用save_info函数，传入info_list参数
    save_info(info_list)


# 定义一个函数decode_num，用于将传入的数字转换为字符串
def decode_num(num):
    # 定义一个字典，用于存储每个数字对应的字符
    num_code = {
        r'\u' + hex(58400)[2:]: '0',
        r'\u' + hex(58381)[2:]: '1',
        r'\u' + hex(58403)[2:]: '2',
        r'\u' + hex(58377)[2:]: '3',
        r'\u' + hex(58397)[2:]: '4',
        r'\u' + hex(58407)[2:]: '5',
        r'\u' + hex(58393)[2:]: '6',
        r'\u' + hex(58365)[2:]: '7',
        r'\u' + hex(58408)[2:]: '8',
        r'\u' + hex(58387)[2:]: '9'
    }
    # 将传入的数字转换为字符串
    new_num = num
    # 遍历字典，将字典中的字符替换为传入的数字
    for j in num_code:
        new_num = new_num.replace(j, num_code[j])
    # 将替换后的字符串转换为utf-8编码，并解码为unicode_escape
    return new_num.encode('utf-8').decode('unicode_escape')


def req_data(form):
    resp = requests.post(url, headers=headers, data=form)
    json_data = json.loads(resp.text)
    data = json_data.get('data', {}).get('series', [])
    return data


def handle_data(data):
    info_list = []
    for i in data:
        brand_name = i.get('brand_name', '')
        outter_name = i.get('outter_name', '')
        car_ids = i.get('car_ids', [])
        dcar_score = i.get('dcar_score', '')
        dealer_price = i.get('dealer_price', '').encode('unicode_escape').decode('utf-8')
        dealer_price = decode_num(dealer_price)
        info_list.append([brand_name, outter_name, car_ids, dcar_score, dealer_price])
    return info_list


def save_info(info_list):
    df = pd.DataFrame(info_list, columns=['brand_name', 'outter_name', 'car_ids', 'dcar_score', 'dealer_price'])
    df.to_csv('../static/data/info.csv', index=False)


if __name__ == '__main__':
    main()
